const mysql = require('mysql2/promise');
const config = require('./database-config.js');

async function addMoreTalentStones() {
    const connection = await mysql.createConnection(config.source);

    console.log('💎 添加更多天赋石用于测试...');

    try {
        // 查询天赋石基础配置
        const [baseStones] = await connection.execute('SELECT * FROM talent_stone_base ORDER BY type_id LIMIT 10');
        console.log(`📋 找到 ${baseStones.length} 个天赋石基础配置`);

        // 为角色2001添加更多天赋石
        const characterId = 2001;
        const newStones = [
            { name: '夺命石', type: 1, level: 1, state: 0, energy: 100 }, // 未激活
            { name: '封锁石', type: 16, level: 1, state: 0, energy: 100 }, // 未激活
            { name: '风沙石', type: 2, level: 1, state: 0, energy: 100 }, // 未激活
            { name: '妖火石', type: 3, level: 1, state: 0, energy: 100 }, // 未激活
            { name: '落雷石', type: 4, level: 1, state: 0, energy: 100 }, // 未激活
        ];

        // 删除现有的天赋石（除了已测试的）
        await connection.execute('DELETE FROM talent_stone WHERE character_id = ? AND id > 2', [characterId]);
        console.log('✅ 清理旧的天赋石数据');

        // 插入新的天赋石
        for (const stone of newStones) {
            await connection.execute(
                'INSERT INTO talent_stone (character_id, name, level, energy, type, state, description) VALUES (?, ?, ?, ?, ?, ?, ?)',
                [characterId, stone.name, stone.level, stone.energy, stone.type, stone.state, `提升${stone.name}威力`]
            );
        }

        console.log(`✅ 添加了 ${newStones.length} 个新天赋石`);

        // 验证结果
        const [allStones] = await connection.execute(
            'SELECT id, name, level, state, energy FROM talent_stone WHERE character_id = ? ORDER BY id',
            [characterId]
        );

        console.log('📋 当前天赋石列表:');
        allStones.forEach(stone => {
            const stateText = ['未激活', '已激活', '已装备到角色', '已装备到副将'][stone.state] || '未知';
            console.log(`  ID=${stone.id}, 名称=${stone.name}, 等级=${stone.level}, 状态=${stateText}, 元气=${stone.energy}`);
        });

    } catch (error) {
        console.error('❌ 添加天赋石失败:', error.message);
    } finally {
        await connection.end();
    }
}

addMoreTalentStones();
